home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / domain.c < prev    next >
C/C++ Source or Header  |  1991-07-17  |  5KB  |  243 lines

  1. /* @(#) $Header: domain.c,v 1.2 91/07/17 14:59:01 deyke Exp $ */
  2.  
  3. #include <sys/types.h>
  4.  
  5. #include <ctype.h>
  6. #include <netdb.h>
  7. #include <netinet/in.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #include <sys/stat.h>
  13.  
  14. #include "global.h"
  15. #include "timer.h"
  16. #include "netuser.h"
  17. #include "cmdparse.h"
  18.  
  19. #define HOSTSFILE       "/tcp/hosts"
  20. #define LOCALDOMAIN     ".ampr.org"
  21.  
  22. struct hosttable {
  23.   struct hosttable *next;
  24.   int32 addr;
  25.   char name[1];
  26. };
  27.  
  28. static int Useserver;
  29. static struct hosttable *hosttable;
  30.  
  31. static void strlwc __ARGS((char *to, const char *from));
  32. static int isaddr __ARGS((const char *s));
  33. static void add_to_hosttable __ARGS((const char *name, int32 addr));
  34. static void load_hosttable __ARGS((void));
  35. static int32 search_name_in_hosttable __ARGS((const char *name));
  36. static int doduseserver __ARGS((int argc, char *argv [], void *p));
  37.  
  38. /*---------------------------------------------------------------------------*/
  39.  
  40. static void strlwc(to, from)
  41. char *to;
  42. const char *from;
  43. {
  44.   while (*to++ = tolower(uchar(*from++))) ;
  45. }
  46.  
  47. /*---------------------------------------------------------------------------*/
  48.  
  49. static int isaddr(s)
  50. const char *s;
  51. {
  52.   int c;
  53.  
  54.   if (s)
  55.     while (c = uchar(*s++))
  56.       if (c != '[' && c != ']' && !isdigit(c) && c != '.') return 0;
  57.   return 1;
  58. }
  59.  
  60. /*---------------------------------------------------------------------------*/
  61.  
  62. static void add_to_hosttable(name, addr)
  63. const char *name;
  64. int32 addr;
  65. {
  66.   struct hosttable *hp;
  67.  
  68.   hp = malloc(sizeof(struct hosttable ) + strlen(name));
  69.   strlwc(hp->name, name);
  70.   hp->addr = addr;
  71.   hp->next = hosttable;
  72.   hosttable = hp;
  73. }
  74.  
  75. /*---------------------------------------------------------------------------*/
  76.  
  77. static void load_hosttable()
  78. {
  79.  
  80.   FILE * fp;
  81.   char *p;
  82.   char line[1024], addr[1024], name[1024];
  83.   int32 n;
  84.   static long lastmtime;
  85.   static long nextchecktime;
  86.   struct hosttable *hp;
  87.   struct stat statbuf;
  88.  
  89.   if (nextchecktime > secclock()) return;
  90.   nextchecktime = secclock() + 60;
  91.   if (stat(HOSTSFILE, &statbuf)) return;
  92.   if (lastmtime == statbuf.st_mtime || statbuf.st_mtime > secclock() - 5)
  93.     return;
  94.   lastmtime = statbuf.st_mtime;
  95.   while (hp = hosttable) {
  96.     hosttable = hosttable->next;
  97.     free(hp);
  98.   }
  99.   if (!(fp = fopen(HOSTSFILE, "r"))) return;
  100.   while (fgets(line, sizeof(line), fp)) {
  101.     if (p = strchr(line, '#')) *p = '\0';
  102.     if (sscanf(line, "%s %s", addr, name) == 2 &&
  103.     isaddr(addr) &&
  104.     (n = aton(addr))) {
  105.       strcat(name, LOCALDOMAIN);
  106.       add_to_hosttable(name, n);
  107.     }
  108.   }
  109.   fclose(fp);
  110. }
  111.  
  112. /*---------------------------------------------------------------------------*/
  113.  
  114. static int32 search_name_in_hosttable(name)
  115. const char *name;
  116. {
  117.   struct hosttable *p, *q;
  118.  
  119.   load_hosttable();
  120.   for (q = 0, p = hosttable; p; q = p, p = p->next)
  121.     if (!strcmp(name, p->name)) {
  122.       if (q) {
  123.     q->next = p->next;
  124.     p->next = hosttable;
  125.     hosttable = p;
  126.       }
  127.       return p->addr;
  128.     }
  129.   return 0;
  130. }
  131.  
  132. /*---------------------------------------------------------------------------*/
  133.  
  134. int32 resolve(name)
  135. char *name;
  136. {
  137.  
  138.   char *p;
  139.   char full_name[1024];
  140.   char lwc_name[1024];
  141.   int32 addr;
  142.   struct hostent *hp;
  143.  
  144.   if (isaddr(name))
  145.     return aton(name);
  146.  
  147.   strlwc(lwc_name, name);
  148.   p = lwc_name + strlen(lwc_name) - 1;
  149.   if (*p == '.') {
  150.     *p = '\0';
  151.     if (addr = search_name_in_hosttable(lwc_name))
  152.       return addr;
  153.     if (Useserver && (hp = gethostbyname(name)))
  154.       return ntohl(((struct in_addr *)(hp->h_addr))->s_addr);
  155.     return 0;
  156.   }
  157.  
  158.   if (!strstr(lwc_name, LOCALDOMAIN)) {
  159.     strcpy(full_name, lwc_name);
  160.     strcat(full_name, LOCALDOMAIN);
  161.     if (addr = search_name_in_hosttable(full_name))
  162.       return addr;
  163.   }
  164.  
  165.   if (addr = search_name_in_hosttable(lwc_name))
  166.     return addr;
  167.   if (Useserver && (hp = gethostbyname(name)))
  168.     return ntohl(((struct in_addr *)(hp->h_addr))->s_addr);
  169.  
  170.   return 0;
  171. }
  172.  
  173. /*---------------------------------------------------------------------------*/
  174.  
  175. char *resolve_a(addr, shorten)
  176. int32 addr;
  177. int shorten;
  178. {
  179.  
  180.   char buf[16];
  181.   struct hostent *hp;
  182.   struct hosttable *p, *q;
  183.   struct in_addr in_addr;
  184.  
  185.   if (!addr)
  186.     return "*";
  187.  
  188.   load_hosttable();
  189.   for (q = 0, p = hosttable; p; q = p, p = p->next)
  190.     if (addr == p->addr) {
  191.       if (q) {
  192.     q->next = p->next;
  193.     p->next = hosttable;
  194.     hosttable = p;
  195.       }
  196.       return hosttable->name;
  197.     }
  198.  
  199.   if (Useserver) {
  200.     in_addr.s_addr = htonl(addr);
  201.     hp = gethostbyaddr((char *) & in_addr, sizeof(in_addr), AF_INET);
  202.     if (hp) {
  203.       add_to_hosttable(hp->h_name, addr);
  204.       return hosttable->name;
  205.     }
  206.   }
  207.  
  208.   sprintf(buf,
  209.       "%u.%u.%u.%u",
  210.       uchar(addr >> 24),
  211.       uchar(addr >> 16),
  212.       uchar(addr >>  8),
  213.       uchar(addr      ));
  214.   add_to_hosttable(buf, addr);
  215.   return hosttable->name;
  216. }
  217.  
  218. /*---------------------------------------------------------------------------*/
  219.  
  220. static struct cmds Dcmds[] = {
  221.   "useserver", doduseserver, 0, 0, NULLCHAR,
  222.   NULLCHAR,    NULLFP,       0, 0, NULLCHAR
  223. };
  224.  
  225. int dodomain(argc, argv, p)
  226. int argc;
  227. char *argv[];
  228. void *p;
  229. {
  230.   return subcmd(Dcmds, argc, argv, p);
  231. }
  232.  
  233. /*---------------------------------------------------------------------------*/
  234.  
  235. static int doduseserver(argc, argv, p)
  236. int argc;
  237. char *argv[];
  238. void *p;
  239. {
  240.   return setbool(&Useserver, "Using server", argc, argv);
  241. }
  242.  
  243.